﻿/** \file
* \$Rev: 187 $
* 
* \$Date: 2010-04-27 21:58:51 +0000 (Tue, 27 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/gui/ManageViewForm.cs $
*/

using System;
using System.Windows.Forms;
using chorus.net;

namespace chorus.gui
{
	/// <summary>
	/// The view management form.
	/// </summary>
	public partial class ManageViewForm : Form
	{
		/// <summary>The currently selected view.</summary>
		net.View ActiveView { get { return (net.View)bindingView.Current; } }
		/// <summary>The currently selected source.</summary>
		SourceContainer ActiveSource { get { return (SourceContainer)bindingSource.Current; } }
		/// <summary>The current message source factory.</summary>
		SourceFactory ActiveFactory { get { return (SourceFactory)bindingSourceFactory.Current; } }
		/// <summary>The currently selected authenticating account.</summary>
		twitter.Account ActiveAccount { get { return (twitter.Account)bindingAccount.Current; } }
		/// <summary>The currently shown source settings panel.</summary>
		SourcePanel currentpanel;

		/// <summary>The constructor.</summary>
		public ManageViewForm()
		{
			InitializeComponent();

			bindingView.DataSource = Program.views;
			bindingAccount.DataSource = Program.accounts;
			bindingSourceFactory.DataSource = SourceFactory.messagefactories;

			foreach (SourceFactory factory in SourceFactory.messagefactories)
			{
				factory.settingspanel.Visible = false;
				groupSourceSettings.Controls.Add(factory.settingspanel);
			}
		}

		#region Event handlers

		/// <summary>Fired when the current view changes.</summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters.</param>
		void bindingView_CurrentChanged(object sender, EventArgs e)
		{
			bool isActive = ActiveView != null;
			groupViewSettings.Enabled = isActive;
			groupSources.Enabled = isActive;
			groupSourceType.Enabled &= isActive;
		}

		/// <summary>Fired when the current source changes.</summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters.</param>
		void bindingSource_CurrentChanged(object sender, EventArgs e)
		{
			bindingSourceFactory.Position = 0;
			bindingAccount.Position = 0;
			groupSourceType.Enabled = ActiveSource != null;
		}

		/// <summary>Fired when the current source factory changes.</summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters.</param>
		void bindingSourceFactory_CurrentChanged(object sender, EventArgs e)
		{
			if (ActiveFactory.needsauth)
				bindingAccount.Remove(twitter.Account.NoAuth);
			else if (!Program.accounts.Contains(twitter.Account.NoAuth))
				bindingAccount.Add(twitter.Account.NoAuth);

			if (currentpanel != null)
				currentpanel.Visible = false;
			currentpanel = ActiveFactory.settingspanel;
			currentpanel.Visible = true;

			bindingSource.ResetCurrentItem();
		}

		/// <summary>
		/// Fired when the authenticating account combo box is validating. Make
		/// sure that an account is selected if the source needs it.
		/// </summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters. e.Cancel is whether to cancel the transaction in question.</param>
		void comboAccount_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = ActiveFactory.needsauth && ActiveAccount == twitter.Account.NoAuth;
		}

		/// <summary>Fired when the form closes. Remove the dummy NoAuth if it's there.</summary>
		/// <param name="sender">The event sender.</param>
		/// <param name="e">The event parameters.</param>
		void ManageViewForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			bindingAccount.Remove(twitter.Account.NoAuth);
		}

		#endregion
	}
}
